package com.org.zp.ctrl.myzhaopin;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.org.base.ctrl.ActivityTaskManager;
import com.org.base.ctrl.BaseActivity;
import com.org.base.util.HttpUtils;
import com.org.base.util.URLHelper;
import com.org.zp.R;
import com.org.zp.app.RecApp;

public class HrLetterDetailActivity extends BaseActivity {
	private TextView headerTitle; // 顶部条标题
	private Button mButtonBack; // 顶部条返回按钮
	private Button mButtonRight; // 顶部条右侧按钮
	
	private TextView subject;
	private TextView company;
	private TextView date_posted;

	private RecApp mApp;
	private ProgressDialog progressDialog;
	private TextView mail_content;// 邮件正文
	
	private String id = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.hr_letter_detail);
		mApp = getMyApplication();
		id = getIntent().getStringExtra("id");
		getComponentId();
		intiEmailData();
		
		new getHrLetterInfo().execute(URLHelper.mPrefix);

//		progressDialog = ProgressDialog.show(getParent(), "提示", "数据加载中,请稍后...",
//				true);
//		progressDialog.setCancelable(true);
	}

	private void intiEmailData() {
		subject = (TextView) findViewById(R.id.subject);
		company = (TextView) findViewById(R.id.company);
		date_posted = (TextView) findViewById(R.id.date_posted);
		mail_content = (TextView) findViewById(R.id.mail_content);
	}

	public void getComponentId() {
		headerTitle = (TextView) findViewById(R.id.headTitle);
		headerTitle.setText("邮件正文");

		mButtonBack = (Button) findViewById(R.id.back);
		mButtonBack.setText("  返回");
		mButtonBack.setOnClickListener(onClickListener);

		mButtonRight = (Button) findViewById(R.id.top_right_button);
		mButtonRight.setVisibility(View.GONE);
	}

	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.back:// 返回
				ActivityTaskManager.getInstance().removeActivity(
						"HrLetterDetailActivity");
				break;
			}
		}
	};

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			ActivityTaskManager.getInstance().removeActivity(
					"HrLetterDetailActivity");
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	class getHrLetterInfo extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
			map.put("act", "interviewNoticeDetail");
			map.put("did", id);
			
			return HttpUtils.readHttp(params[0], map);// 网络操作，向服务器发送登录请求，获取返回信息

		}

		@Override
		protected void onPostExecute(String result) {

			if (result == null || result.equals("")) {
				// 发送解析数据 消息
				Toast toast = Toast.makeText(HrLetterDetailActivity.this,
						"失败", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				
			} else if (result.equals("timeout")) {
				// 发送解析数据 消息
				Toast toast = Toast.makeText(HrLetterDetailActivity.this,
						"网络超时", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				
			} else {
				parseJson(result);
			}

		}
	}

	private void parseJson(String str) {
		try {
			String succeed = new JSONObject(str).getString("succeed");
			if (succeed.equals("000")) {
				JSONObject json = new JSONObject(str);
				
				subject.setText(json.getString("jobs_name"));
				company.setText(json.getString("company_name"));
				date_posted.setText(json.getString("interview_addtime"));
				mail_content.setText(json.getString("notes"));
				
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
}
